package edu.albany.csi518.test;

import edu.albany.csi518.util.CustomFileReader;
import edu.albany.csi518.util.DbUtil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.TreeMap;

/**
 * A class dedicated to simple code testing
 *
 * @author John M. Lien
 */
public class Sandbox {

    /**
     * This class is only used for testing code (No Comments Needed).
     *
     * @param args
     */
    private static void assertArrayLength(String[] split) {
        if (split.length < 2) {
            System.out.println("Error in file formatting");
            System.out.println(Arrays.deepToString(split));
            System.exit(1);
        }
    }

    public static void main(String... args) {
        /* variables */
        String strLine;
        String[] split, smaller_split;
        DbUtil db = new DbUtil();
        TreeMap<String, HashSet<String>> map = new TreeMap();
        CustomFileReader read = new CustomFileReader("/home/administrator/Downloads/disease-medicine.csv");

        /* read all lines in the file */
        while (read.hasNextLine()) {
            strLine = read.getNextLine();
            split = strLine.split("\t"); //split each of the medicines into their own index set
            assertArrayLength(split); //check the length of each disease
            if (!map.containsKey(split[0])) { //make sure the disease doesn't exist already in the map
                map.put(split[0], new HashSet()); //add the disease to the map
                for (int i = 1; i < split.length; i++) { //re-split each medicine set into a list of individual medicines
                    map.get(split[0]).addAll(Arrays.asList(split[i].replaceAll("[\\s+]", " ").replace(", ", ",").trim().split(","))); //add the list of medicines to the disease
                }
            } else {
                System.out.println("Key should not exist twice");
                System.exit(1);
            }
        }

        HashSet<String> list = new HashSet();
        /* read through the map */
        for (String key : map.keySet()) {
            /* read each medicine corresponding to the disease */
            for (String tok : map.get(key)) {
                if(!list.contains(tok)){
                     list.add(tok);
                }               
                //System.out.println(key + "," + tok); //print the relationship.
            }
        }
        for (String tok : list) {
            tok = tok.trim();
            System.out.println(tok);
            db.Update("insert into Medicine(medicine_name) values(?)", tok);
        }
    }/*End Program */

}
